package com.zjweu.hkg.Controller;

import com.zjweu.hkg.DAO.TestDAO;

import com.zjweu.hkg.POJO.Test;
import com.zjweu.hkg.POJO.TestMultiple;
import com.zjweu.hkg.POJO.User;
import com.zjweu.hkg.Servies.UserServices;
import com.zjweu.hkg.WebSocket.CounterWsServerEndPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/test")
public class TestController {

    @Autowired
    TestDAO testDAO;
    @Autowired
    CounterWsServerEndPoint counterWsServerEndPoint;
    @Autowired
    UserServices userServices;
    @RequestMapping("/save")
    public Test save(@RequestParam(value = "name") String name) {
        Test test=new Test(name);
        System.out.println("test"+test.toString());
        return testDAO.save(test);
    }
    @RequestMapping("/print")
    public String print(){
        System.out.println("联通了！");

        return "联通了！";
    }
    @RequestMapping("/counter")
    void counter() throws Exception {
        for (int i = 1; i <= 100; i++) {
            // 模拟耗时任务
            Thread.sleep(500);
            System.out.println(i);
            // 推送进度
            counterWsServerEndPoint.sendMessage(String.valueOf(i));
        }
        }
    @RequestMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        // 目标 URL
        String targetUrl = "http://47.98.117.127:8083/netdisk/api/upload";
//        String targetUrl = "http://localhost:8083/netdisk/api/upload";
        // 构建请求体

        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", file.getResource());
        body.add("fileName", "fileName.txt");
        body.add("bucketName", "diary");

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(org.springframework.http.MediaType.MULTIPART_FORM_DATA);

        // 创建 HttpEntity 封装请求头和请求体
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

        // 创建 RestTemplate 实例
        RestTemplate restTemplate = new RestTemplate();

        // 发送 POST 请求到目标服务器
        ResponseEntity<String> response = restTemplate.exchange(targetUrl, HttpMethod.POST, requestEntity, String.class);

        // 返回响应内容
        return response;
    }
   @RequestMapping("/testTag")
    public void testTag(@RequestBody Map<String, Object> requestData){
       List tag= (ArrayList) requestData.get("tag");
       System.out.println(tag);
   }
    @RequestMapping("/testMultiple")
    public TestMultiple testMultiple(){
        User user=userServices.getUserById(1);
        System.out.println(user);
        TestMultiple testMultiple=new TestMultiple(10,"浙江省",user);
        return testMultiple;
    }

}

